#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdbool.h>
#include<strings.h>
#include<errno.h>
#include<time.h>
#include<string.h>

int main(int argc, char const *argv[])
{
    FILE *fp = fopen("time.txt", "a+");

    int line_num = 0;
    FILE *fp_line;
    if(access("line.txt", F_OK))
        fp_line = fopen("line.txt", "w+");
    else{
        fp_line = fopen("line.txt", "r+");
        line_num = fgetc(fp_line);
    }

    char buf[50];
    while(1){
        time_t t = time(NULL);
        snprintf(buf, 50, "%d, %s", ++line_num, ctime(&t));
        printf("%s", buf);
        fputs(buf, fp);
        fflush(fp);

        fseek(fp_line, 0, SEEK_SET);
        fwrite(&line_num, sizeof(int), 1, fp_line);
        fflush(fp_line);
        sleep(1);
    }
    fclose(fp);
    fclose(fp_line);
    return 0;
}
